package com.example.ssm1.controller;

import com.example.ssm1.exception.BusinessException;
import com.example.ssm1.model.User;
import com.example.ssm1.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
@AllArgsConstructor
public class LoginController {

    private final IUserService userService;

    @GetMapping("/login")
    public String login(){
        return "login";
    }
    @PostMapping("/login")
    public String loginPost(String username, String password, Model model, HttpServletRequest request) throws BusinessException {
            User user= userService.getUserByName(username, password);
            HttpSession session = request.getSession();
            session.setAttribute("LoginUser",user);
        return "redirect:user/list";
    }

}
